from apps.users.models import User
from rest_framework import serializers

# class UserSerializer(serializers.ModelSerializer):
#
#     class Meta:
#         model = User
#         fields = ['id','username','mobile','email']

class UserAddSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ['username','password','email','mobile','id']

    extra_kwargs={
        "username":{
            'max_length':20,
            'min_length':5
        },
        'password':{
            'max_length':20,
            'min_length':8,
            'write_only':True,
        }
    }
    #重写create方法，因为保存用户数据需要对密码加密
    def create(self, validated_data):
        user = User.objects.create_user(**validated_data)
        return user